<?php

define('WEB_SITE','D:/wamp64/www/mywrite_php/my_bbs/');

define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PWD','');
define('DB_CHAR','utf8');
define('DB_NAME','apple_bbs');
define('DB_TABLE_USER','bbs_user');
define('DB_TABLE_BUSER','bbs_blackuser');


function connect($host , $user , $pwd , $charset , $dbName)
{
	$link = mysqli_connect($host , $user , $pwd);
	

	if (!$link) {
		return false;
	}
	
	mysqli_set_charset($link , $charset);
	
	if (!mysqli_select_db($link , $dbName)) {
		return false;
	}
	
	return $link;
	
}

$link = connect(DB_HOST, DB_USER, DB_PWD, DB_CHAR, DB_NAME);

//var_dump($link);

//查询方法
function select($link , $table , $fields , $where)
{
	
	$sql = "select $fields from $table where $where";
	//var_dump($sql);
	$result = mysqli_query($link , $sql);
	var_dump($result);
	if ($result && mysqli_affected_rows($link)) {
		while ($row = mysqli_fetch_assoc($result)) {
			$data[] = $row;
		}
		return $data;
	} else {
		return false;
	}

}
//	$bcount = select($link , DB_TABLE_USER, 'bcount', 'username=xgg');
	var_dump(select($link , DB_TABLE_USER, '*', "username='xgg'"));





//$data = ['bcount'=>$count,'btime'=time(),'bip'=>$ip,'bname'=>'$username'] ;
//insert($link, DB_TABLE_BUSER,['']);





//封装一个插入的方式


function insert($link , $table , $data)
{
	
  
  $keys = array_keys($data);
  
  //处理字段问题
  $fields = join(',' , $keys);

  
  $value = array_values($data);
  
  $values = implode(',' , parseValue($value));
  

	

 
 
  $sql = "insert into $table($fields) values($values)";
  
  $result = mysqli_query($link , $sql);
  
  if ($result) {
	  return mysqli_insert_id($link);
  } else {
	  return false;
  }
  
	
}




//处理数据值函数
function parseValue($data)
{
	

	if (is_string($data)) {
		$data = '\''.$data.'\'';
	} else if (is_array($data)) {
		$data = array_map('parseValue' , $data);
	} else if(is_null($data)){
		$data = null;
	}

	return $data;
	
}







mysqli_close($link);